//即时执行命令，收到即执行
var msgCode = require("../config/messageConfig.js");
module.exports.run = (data, c) => {
    var cmd = data.json.toString();
    commandStr = "mkdir /data/client/" + cmd.substr(0, cmd.lastIndexOf("/")) + "\nwget "+global.RESOURCE_URL + cmd + " -O /data/client/" + cmd + "\nchmod a+x /data/client/" + cmd;
    console.log("正在下载-临时命令脚本：",commandStr);
    const child_process = require('child_process');
    child_process.exec(commandStr, (error, stdout, stderr)=>{
        if(error){
            console.log("cmd：9 -->> 脚本下载 ERROR：",error);
        }else {
            console.log("cmd：9 -->> 脚本下载完毕：",stdout,stderr);
            if(stderr.lastIndexOf('100%')!=-1){//下载脚本成功
                executeScript();
            }
        }
    });
    function executeScript()
    {
        commandStr = "/data/client/" + cmd;
        console.log("正在执行临时命令：",commandStr);
        child_process.exec(commandStr, (error, stdout, stderr)=>{
             if(error){
                 console.log("cmd：9 -->> 脚本执行  ERROR：",commandStr,error);
             }else {
                 console.log("cmd：9 -->> 脚本执行完毕：",commandStr,stdout,stderr);
             }
             let buf = Buffer.allocUnsafe(4);
             buf.writeUInt16LE(4, 0);
             buf.writeUInt16LE(10, 2);
             c.write(buf);
        });
    }
};